// PARADIME INTERPRETER
//
//LICENSED FREELY UNDER MIT.
//CREATED FOR FREE USE.
#include "stdafx.h"
#include "variabledomain.h"

VariableDomain::VariableDomain(Interpreter* PInterpreter, VariableDomain* ParentDomain){

	this->State = VARDOM_READY;
	this->PInterpreter = PInterpreter;
	this->AllowRecursiveLookups = true; //Default
	this->ParentDomain = ParentDomain;

}

void VariableDomain::Assign(std::string input, Variant* var){
	std::map<std::string,Variant*>::iterator temp = this->Variabletable.find(input);
	if(temp==this->Variabletable.end()){
			this->Variabletable[input] = var;
	}else{
			temp->second->StorageReset();
			temp->second = var;
	};

}

//Returns null pointer if not found.
//Follows recursive lookups if set to.
Variant* VariableDomain::Lookup(std::string name){
	std::map<std::string,Variant*>::iterator temp = this->Variabletable.find(name);
	if(temp==this->Variabletable.end()){
		if(this->AllowRecursiveLookups){
			if(this->ParentDomain)this->ParentDomain->Lookup(name);
		}
	}else{
		return temp->second;
	}
	return (Variant*)0;
}

void VariableDomain::COUTdump(void){
	std::cout << "Variable Dump:\n\n";

	std::map<std::string,Variant*>::iterator temp = this->Variabletable.begin();
	for(;temp!=this->Variabletable.end();++temp){
		std::cout << temp->first << ":::";
		std::cout << temp->second->VarType;
		if(VAR_INT32==temp->second->VarType)std::cout << " " << temp->second->GetInt32();
		if(VAR_STRING==temp->second->VarType)std::cout << " " << temp->second->GetString();
		if(VAR_DOUBLE==temp->second->VarType)std::cout << " " << temp->second->GetDouble();
		std::cout << '\n';
	}

}